package com.zzuli.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzuli.dao.DeviceInfoDao;
import com.zzuli.entity.DeviceInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceInfoService {
    @Autowired
    private DeviceInfoDao deviceInfoDao;

    public Map<String, Object> selectAll(int pageNum, int pageSize, DeviceInfoEntity deviceInfoEntity) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<DeviceInfoEntity> list = deviceInfoDao.selectAll(deviceInfoEntity);
        //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<DeviceInfoEntity> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("list", list);
        //总数据条目
        map.put("total", total);
        return map;
    }

    public boolean addDevice(DeviceInfoEntity deviceInfoEntity) {
        return deviceInfoDao.insertSelective(deviceInfoEntity);
    }

    public boolean updateDevice(DeviceInfoEntity deviceInfoEntity) {
        return deviceInfoDao.updateByPrimaryKeySelective(deviceInfoEntity);
    }

    public boolean deleteDevice(String[] allDeviceId) {
        for(String deviceId : allDeviceId){
            deviceInfoDao.deleteByPrimaryKey(Integer.parseInt(deviceId));
        }
        return true;
    }
}
